<template>
	<div class="system-dict-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="569px">
			<el-form ref="dictDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="state.rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字典类型">
							<el-input v-model="state.ruleForm.dictType" :disabled="true" placeholder="请输入字典类型"
								clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字典标签" prop="dictLabel">
							<el-input v-model="state.ruleForm.dictLabel" placeholder="请输入字典标签" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="数据键值" prop="dictValue">
							<el-input v-model="state.ruleForm.dictValue" placeholder="请输入数据键值" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字典状态">
							<el-radio-group v-model="state.ruleForm.status">
								<el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{
									dict.label }}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="样式属性">
							<el-input v-model="state.ruleForm.cssClass" placeholder="请输入样式属性" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="回显样式">
							<el-select v-model="state.ruleForm.listClass">
								<el-option v-for="item in state.listClassOptions" :key="item.value"
									:label="item.label + '(' + item.value + ')'" :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字典排序">
							<el-input-number v-model="state.ruleForm.dictSort" :min="0" :max="9999"
								controls-position="right" placeholder="请输入字典排序"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.remark" type="textarea" placeholder="请输入备注"
								clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" v-auth="'system:dict:edit'">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDictDataDialog">
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useDictData } from '/@/api/system/dict/data';
import { useDict } from '/@/utils/dict';

const { sys_normal_disable } = useDict("sys_normal_disable");
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dictDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		createBy: '',
		createTime: '',
		updateBy: '',
		updateTime: '',
		dictCode: '',
		dictSort: 0,
		dictLabel: '',
		dictValue: '',
		dictType: '',
		cssClass: '',
		listClass: '',
		isDefault: '',
		status: '',
		remark: '',
		default: false,
	} as RowDictDataType,
	listClassOptions: [
		{
			value: "default",
			label: "默认"
		},
		{
			value: "primary",
			label: "主要"
		},
		{
			value: "success",
			label: "成功"
		},
		{
			value: "info",
			label: "信息"
		},
		{
			value: "warning",
			label: "警告"
		},
		{
			value: "danger",
			label: "危险"
		}
	],
	rules: {
		dictLabel: [
			{ required: true, message: '请输入字典标签', trigger: 'blur' },
			{ max: 100, message: '长度在 100 个字符内', trigger: 'blur' }
		],
		dictValue: [
			{ required: true, message: '请输入数据键值', trigger: 'blur' },
			{ max: 100, message: '长度在 100 个字符内', trigger: 'blur' }
		],
		dictSort: [
			{ required: true, message: '请输入字典排序', trigger: 'blur' },
			{ type: 'number', message: '必须为数字值' },
		],
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: RowDictDataType, dictType: string) => {
	state.dialog.type = type;
	state.ruleForm.dictType = dictType;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改字典';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增字典';
		state.dialog.submitTxt = '新 增';
		getDictRuleForm();

	}
	state.dialog.isShowDialog = true;
};

// 提交
const onSubmit = async () => {
	try {
		await dictDialogFormRef.value.validate(async (valid: any, fields: any) => {
			if (valid) {
				if (state.dialog.type === 'add') {
					await addDict();
				} else {
					await updateDict();
				}
			} else {
				ElMessage.error('请检查表单必填项');
				return Promise.reject(fields);
			}
		});
	} catch (err) {
		Promise.reject(err);
	}
};
const addDict = async () => {
	try {
		const res = await useDictData().addData({
			dictLabel: state.ruleForm.dictLabel,
			dictValue: state.ruleForm.dictValue,
			listClass: state.ruleForm.listClass,
			dictSort: state.ruleForm.dictSort,
			status: state.ruleForm.status,
			remark: state.ruleForm.remark,
			dictType: state.ruleForm.dictType,
		});
		if (res.code === 200) {
			ElMessage.success(res.msg);
			closeDialog();
			emit('refresh');
		} else {
			ElMessage.error(res.msg);
		}
	} catch (err) {
		Promise.reject(err);
	}
};
const updateDict = async () => {
	try {
		const res = await useDictData().updateData(state.ruleForm);
		if (res.code === 200) {
			ElMessage.success(res.msg);
			closeDialog();
			emit('refresh');
		} else {
			ElMessage.error(res.msg);
		}
	} catch (err) {
		Promise.reject(err);
	}
};


// 初始化字典数据
const getDictRuleForm = () => {
	state.ruleForm = {
		createBy: '',
		createTime: '',
		updateBy: '',
		updateTime: '',
		dictCode: '',
		dictSort: 0,
		dictLabel: '',
		dictValue: '',
		dictType: state.ruleForm.dictType,
		cssClass: '',
		listClass: '',
		isDefault: '',
		status: '',
		remark: '',
		default: false,
	};
};
// 取消
const onCancel = () => {
	closeDialog();
	getDictRuleForm();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};// 暴露变量
defineExpose({
	openDialog,
});
</script>